package it.patamau.bukkit.deathmatch;

import java.io.IOException;
import java.util.logging.Level;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.plugin.PluginManager;

import it.patamau.bukkit.AbstractPlugin;

public class DMPlugin extends AbstractPlugin {
	
	protected DMParameters parameters;
	private DMPlayerListener playerListener;
	private DMEntityListener entityListener;
	private DMPlayerStats stats;
	
	public DMPlugin(){
		super();		
		parameters = new DMParameters(this);
		stats = new DMPlayerStats();
		playerListener = new DMPlayerListener(this);
		entityListener = new DMEntityListener(this);
	}
	
	public DMParameters getParameters(){
		return parameters;
	}
	
	public void onDamage(Player player, DamageCause cause, Player damager){
		DMPlayerStat p = getStats(player.getName());
		p.setDamagecause(cause);
		//reset damager if none has been specified
		if(damager==null){
			p.setDamager(null);
		}else{
			p.setDamager(damager.getName());
		}
	}
	
	public void onDeath(Player player) {
		DMPlayerStat stats = getStats(player.getName());
		stats.setDeaths(stats.getDeaths()+1);
		if(stats.getDamager()!=null){
			//killed
			DMPlayerStat killerstats = getStats(stats.getDamager());
			killerstats.setKills(killerstats.getKills()+1);
			sendMessage("dm.player.killed", player, stats.getDamager());
			broadcastMessage("dm.kill", player, stats.getName(), stats.getDamager());
		}else{
			//casual death
			sendMessage("dm.player.dead", player, stats.getDamagecause().name());
			broadcastMessage("dm.death", player, stats.getName(), stats.getDamagecause().name());
		}
	}
	
	public DMPlayerStat getStats(String playerName){
		return stats.get(playerName);
	}
	
	public boolean isStarted(){
		return parameters.dmstarted;
	}
	
	public void onEnable(){
		initialize(parameters);
		super.onEnable();
		try {
			parameters.load();
		} catch (IOException e) {
			log(Level.WARNING,e.getMessage());
			try {
				parameters.store();
			} catch (IOException e1) {
				log(Level.SEVERE,e1.getMessage());
			}
		}
		
        // register events
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
        //pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, _playerListener, Priority.Normal, this);
        
        pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.Normal, this);
        //pm.registerEvent(Event.Type.ENTITY_DAMAGED, _entityListener, Priority.Normal, this);
        //pm.registerEvent(Event.Type.ENTITY_COMBUST, _entityListener, Priority.Normal, this);
        //pm.registerEvent(Event.Type.ENTITY_DEATH, _entityListener, Priority.Normal, this);        
	}
	
	public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
		if(sender.isOp()){
			if(args.length>0){
				if(args[0].equals("reload")){
					initialize(parameters);
				}
			}
		}
		return true;
	}

	public void onDisable(){
		super.onDisable();
		try {
			parameters.store();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
